package org.holynite.fieldmanage.services

import org.holynite.fieldmanage.Utils.ManagementException
import org.holynite.fieldmanage.ejbs.AdministratorManagement
import org.holynite.fieldmanage.entities.Administrator
import org.holynite.fieldmanage.persistence.AdministratorEAO
import org.holynite.fieldmanage.persistence.AdministratorRoleEAO
import org.holynite.fieldmanage.types.Exceptions

import javax.ejb.EJB
import javax.ejb.Stateless

/**
 * Created by midnite on 15/1/10.
 */
@Stateless(name = "adminControl", mappedName = "adminControlBean")
class AdministratorControlBean implements AdministratorManagement {

	@EJB(beanName = "adminEAO")
	AdministratorEAO adminEao

	Administrator defaultAdministrator

	AdministratorControlBean() {
		defaultAdministrator = new Administrator(
			workdId: "ROOT", name: "vmadmin", password: "vmadmin"
		)
	}

	@Override
	void addAdministrator(Administrator newAdmin) throws ManagementException {
		try {
			newAdmin.with {
				if (role) correspondRole = role.svrid
				if (!lastLogin) lastLogin = Calendar.instance.time
			}
			adminEao.save(newAdmin)
		} catch (Exception e) {
			throw new ManagementException(Exceptions.Administration)
		}
	}

	@Override
	void saveAdministrator(Administrator admin) throws ManagementException {
		try {
			if (admin.role) admin.correspondRole = admin.role.svrid
			adminEao.update(admin)
		} catch (Exception e) {
			throw new ManagementException(Exceptions.Administration)
		}
	}

	@Override
	void removeAdministrator(Object key) throws ManagementException {
		try {
			adminEao.delete(Administrator, key)
		} catch (Exception e) {
			throw new ManagementException(Exceptions.Administration)
		}
	}

	@Override
	int checkAdministratorAmount() {
		try {
			adminEao.checkAdministratorAmount()
		} catch (Exception e) {
			-1
		}
	}

	@Override
	Administrator checkLogin(String adminWorkId, String password) throws ManagementException {
		try {
			Administrator admin = adminEao.get(Administrator, adminWorkId)
			if (admin.password.equals(password)) {
				admin
			} else {
				throw new Exception()
			}
		} catch (Exception e) {
			throw new ManagementException(Exceptions.LoginFailed)
		}
	}

	@Override
	Administrator checkDefaultLogin(String adminWorkId, String password) throws ManagementException {
		if (defaultAdministrator.workdId.equals(adminWorkId) && defaultAdministrator.password.equals(password)) {
			defaultAdministrator
		} else {
			throw new ManagementException(Exceptions.LoginFailed)
		}
	}

	@Override
	Boolean checkPassword(Administrator admin, String password) throws ManagementException {
		try {
			admin.password.equals(password)
		} catch (Exception) {
			throw new ManagementException(Exceptions.Administration)
		}
	}

	@Override
	Boolean isDefaultAdministrator(Administrator admin) {
		admin.equals(defaultAdministrator)
	}

	@Override
	void changePassword(Administrator admin) throws ManagementException {
		try {
			adminEao.update(admin)
		} catch (Exception e) {
			throw new ManagementException(Exceptions.Administration)
		}
	}

	@Override
	List<Administrator> listAdministrators(String keyword) {
		adminEao.searchAdministrators(keyword)
	}

	@Override
	int checkAvaliableAdministratorAmout() {
		adminEao.checkAvaliableAdministrator()
	}

	@Override
	Boolean checkWorkIdUsable(String workId) {
		adminEao.checkWorkIdUsable(workId)
	}
}
